package core

import (
	"sync"
	"time"
)

// DelayMonitor 延迟监控器
type DelayMonitor struct {
	// 各协议组件的延迟记录
	RBCDelays    map[int]time.Duration // [SN]延迟
	ABADelays    map[int]time.Duration // [SN]延迟
	MVBADelays   map[int]time.Duration // [SN]延迟
	ACSDelays    map[int]time.Duration // [SN]延迟
	
	// 各协议组件的开始时间
	RBCStartTimes    map[int]time.Time // [SN]开始时间
	ABAStartTimes    map[int]time.Time // [SN]开始时间
	MVBAStartTimes   map[int]time.Time // [SN]开始时间
	ACSStartTimes    map[int]time.Time // [SN]开始时间
	
	// 统计信息
	TotalRBCDelays    []time.Duration
	TotalABADelays    []time.Duration
	TotalMVBADelays   []time.Duration
	TotalACSDelays    []time.Duration
	
	// 锁
	mutex sync.RWMutex
}

// NewDelayMonitor 创建延迟监控器
func NewDelayMonitor() *DelayMonitor {
	return &DelayMonitor{
		RBCDelays:      make(map[int]time.Duration),
		ABADelays:      make(map[int]time.Duration),
		MVBADelays:     make(map[int]time.Duration),
		ACSDelays:      make(map[int]time.Duration),
		RBCStartTimes:  make(map[int]time.Time),
		ABAStartTimes:  make(map[int]time.Time),
		MVBAStartTimes: make(map[int]time.Time),
		ACSStartTimes:  make(map[int]time.Time),
		TotalRBCDelays: make([]time.Duration, 0),
		TotalABADelays: make([]time.Duration, 0),
		TotalMVBADelays: make([]time.Duration, 0),
		TotalACSDelays: make([]time.Duration, 0),
	}
}

// StartRBC 开始RBC延迟计时
func (dm *DelayMonitor) StartRBC(sn int) {
	dm.mutex.Lock()
	defer dm.mutex.Unlock()
	dm.RBCStartTimes[sn] = time.Now()
}

// EndRBC 结束RBC延迟计时
func (dm *DelayMonitor) EndRBC(sn int) time.Duration {
	dm.mutex.Lock()
	defer dm.mutex.Unlock()
	
	if startTime, exists := dm.RBCStartTimes[sn]; exists {
		delay := time.Since(startTime)
		dm.RBCDelays[sn] = delay
		dm.TotalRBCDelays = append(dm.TotalRBCDelays, delay)
		delete(dm.RBCStartTimes, sn)
		return delay
	}
	return 0
}

// StartABA 开始ABA延迟计时
func (dm *DelayMonitor) StartABA(sn int) {
	dm.mutex.Lock()
	defer dm.mutex.Unlock()
	dm.ABAStartTimes[sn] = time.Now()
}

// EndABA 结束ABA延迟计时
func (dm *DelayMonitor) EndABA(sn int) time.Duration {
	dm.mutex.Lock()
	defer dm.mutex.Unlock()
	
	if startTime, exists := dm.ABAStartTimes[sn]; exists {
		delay := time.Since(startTime)
		dm.ABADelays[sn] = delay
		dm.TotalABADelays = append(dm.TotalABADelays, delay)
		delete(dm.ABAStartTimes, sn)
		return delay
	}
	return 0
}

// StartMVBA 开始MVBA延迟计时
func (dm *DelayMonitor) StartMVBA(sn int) {
	dm.mutex.Lock()
	defer dm.mutex.Unlock()
	dm.MVBAStartTimes[sn] = time.Now()
}

// EndMVBA 结束MVBA延迟计时
func (dm *DelayMonitor) EndMVBA(sn int) time.Duration {
	dm.mutex.Lock()
	defer dm.mutex.Unlock()
	
	if startTime, exists := dm.MVBAStartTimes[sn]; exists {
		delay := time.Since(startTime)
		dm.MVBADelays[sn] = delay
		dm.TotalMVBADelays = append(dm.TotalMVBADelays, delay)
		delete(dm.MVBAStartTimes, sn)
		return delay
	}
	return 0
}

// StartACS 开始ACS延迟计时
func (dm *DelayMonitor) StartACS(sn int) {
	dm.mutex.Lock()
	defer dm.mutex.Unlock()
	dm.ACSStartTimes[sn] = time.Now()
}

// EndACS 结束ACS延迟计时
func (dm *DelayMonitor) EndACS(sn int) time.Duration {
	dm.mutex.Lock()
	defer dm.mutex.Unlock()
	
	if startTime, exists := dm.ACSStartTimes[sn]; exists {
		delay := time.Since(startTime)
		dm.ACSDelays[sn] = delay
		dm.TotalACSDelays = append(dm.TotalACSDelays, delay)
		delete(dm.ACSStartTimes, sn)
		return delay
	}
	return 0
}

// GetAverageDelays 获取平均延迟
func (dm *DelayMonitor) GetAverageDelays() map[string]time.Duration {
	dm.mutex.RLock()
	defer dm.mutex.RUnlock()
	
	averages := make(map[string]time.Duration)
	
	// 计算RBC平均延迟
	if len(dm.TotalRBCDelays) > 0 {
		var total time.Duration
		for _, delay := range dm.TotalRBCDelays {
			total += delay
		}
		averages["RBC"] = total / time.Duration(len(dm.TotalRBCDelays))
	}
	
	// 计算ABA平均延迟
	if len(dm.TotalABADelays) > 0 {
		var total time.Duration
		for _, delay := range dm.TotalABADelays {
			total += delay
		}
		averages["ABA"] = total / time.Duration(len(dm.TotalABADelays))
	}
	
	// 计算MVBA平均延迟
	if len(dm.TotalMVBADelays) > 0 {
		var total time.Duration
		for _, delay := range dm.TotalMVBADelays {
			total += delay
		}
		averages["MVBA"] = total / time.Duration(len(dm.TotalMVBADelays))
	}
	
	// 计算ACS平均延迟
	if len(dm.TotalACSDelays) > 0 {
		var total time.Duration
		for _, delay := range dm.TotalACSDelays {
			total += delay
		}
		averages["ACS"] = total / time.Duration(len(dm.TotalACSDelays))
	}
	
	return averages
}

// GetDelayStats 获取延迟统计信息
func (dm *DelayMonitor) GetDelayStats() map[string]interface{} {
	dm.mutex.RLock()
	defer dm.mutex.RUnlock()
	
	stats := make(map[string]interface{})
	
	// RBC统计
	stats["RBC"] = map[string]interface{}{
		"count": len(dm.TotalRBCDelays),
		"delays": dm.TotalRBCDelays,
	}
	
	// ABA统计
	stats["ABA"] = map[string]interface{}{
		"count": len(dm.TotalABADelays),
		"delays": dm.TotalABADelays,
	}
	
	// MVBA统计
	stats["MVBA"] = map[string]interface{}{
		"count": len(dm.TotalMVBADelays),
		"delays": dm.TotalMVBADelays,
	}
	
	// ACS统计
	stats["ACS"] = map[string]interface{}{
		"count": len(dm.TotalACSDelays),
		"delays": dm.TotalACSDelays,
	}
	
	return stats
}
